<?php

class CompanyGoodsBehavior extends CActiveRecordBehavior{

	public $criteria = array();
	public $modelName;
	public $companyId;
	
	public function getGoods() {
		$owner = $this->owner;
		$criteria = new CDbCriteria($this->criteria);
		$criteria->compare($this->companyId, $owner->id);
		$criteria->with = array('goods');
//		$criteria->join = 'left join v_goods_agentable as v on (t.goods_id = v.goods_id)';
		$criteria->compare('goods.listing_status','ST_LISTING');
		$criteria->compare('goods.status','ST_PASSED');
//		$criteria->addCondition('v.id IS NOT NULL');
		$criteria->group = 't.id';
		$model = new $this->modelName();
		return $model->findAll($criteria);
	}
	
	public function noGoods() {
		$model = $this->owner;
		$criteria = $model->getDbCriteria();
		$target = new $this->modelName;
		$tablename = $target->tablename();
//		$sql = "(SELECT COUNT(1) FROM `{$tablename}` as r
//			left join v_goods_agentable as v on (v.goods_id = r.goods_id)
//			 WHERE `{$this->companyId}` = t.id > 0 and v.id is not null)";
//		$criteria->addCondition($sql);
		$criteria->order = "(SELECT COUNT(1) FROM `{$tablename}` as r
			left join v_goods_agentable as v on (v.goods_id = r.goods_id)
			 WHERE `{$this->companyId}` = t.id > 0 and v.id is not null) desc";
		return $model;
	}
}

?>